package com.cpi.cornpp.distributionpotoplants.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.distributionpotoplants.exception.DistributionPOToPlantException;
import com.cpi.cornpp.distributionpotoplants.service.DistributionPOToPlantService;

public class DistributionPOToPlantAction extends MappingDispatchAction {
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Customer exception*/
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "distribution.add.success";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "distribution.delete.success";//$NON-NLS-1$
	/** Version id in session */
	private static final String VERSION_ID = "versionId";//$NON-NLS-1$
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws DistributionPOToPlantException {
		HttpSession session = null;
		String message = null;
		MessageResources messageResources = null;
		DistributionPOToPlantService distributionPOToPlantService = null;
		String versionId = null;
		try{
			session = request.getSession(true);
			messageResources = getResources(request);
			distributionPOToPlantService = new DistributionPOToPlantService();
			versionId = (String) session.getAttribute(VERSION_ID);
			distributionPOToPlantService.add(Integer.parseInt(versionId));
			message = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
		} catch (DistributionPOToPlantException distributionPOToPlantException) {
			throw distributionPOToPlantException;
		}catch(Exception exception){
			DistributionPOToPlantException distributionPOToPlantException=null;
			exception.printStackTrace(System.out);
			distributionPOToPlantException= new DistributionPOToPlantException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			session.setAttribute(CUSTOM_EXCEPTION, distributionPOToPlantException);
			throw distributionPOToPlantException;
		}
		session.setAttribute(MESSAGE, message);
		return mapping.findForward(SUCCESS_FORWARD);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws DistributionPOToPlantException {
		HttpSession session = null;
		String message = null;
		MessageResources messageResources = null;
		DistributionPOToPlantService distributionPOToPlantService = null;
		String versionId = null;
		try{
			session = request.getSession(true);
			messageResources = getResources(request);
			distributionPOToPlantService = new DistributionPOToPlantService();
			versionId = (String) session.getAttribute(VERSION_ID);
			distributionPOToPlantService.delete(Integer.parseInt(versionId));
			message = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
		} catch (DistributionPOToPlantException distributionPOToPlantException) {
			throw distributionPOToPlantException;
		}catch(Exception exception){
			DistributionPOToPlantException distributionPOToPlantException=null;
			exception.printStackTrace(System.out);
			distributionPOToPlantException= new DistributionPOToPlantException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			session.setAttribute(CUSTOM_EXCEPTION, distributionPOToPlantException);
			throw distributionPOToPlantException;
		}
		session.setAttribute(MESSAGE, message);
		return mapping.findForward(SUCCESS_FORWARD);
	}
}